package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.PatientMapper;
import com.ssy.bus.pojo.Patient;
import com.ssy.bus.service.IPatientService;
import com.ssy.bus.vo.PatientVo;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ssy
 */
@Service
public class IPatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public PageResult selectPatientByPage(PatientVo patientVo) {
        Page<Patient> page = new Page<>(patientVo.getPage(), patientVo.getLimit());
        IPage<Patient> patientIPage = this.patientMapper.selectPage(page, new LambdaQueryWrapper<Patient>()
                .like(StringUtils.hasText(patientVo.getName()), Patient::getName, patientVo.getName())
                .like(StringUtils.hasText(patientVo.getPhone()), Patient::getPhone, patientVo.getPhone())
                .eq(StringUtils.hasText(patientVo.getIdNumber()), Patient::getIdNumber, patientVo.getIdNumber())
                .orderByDesc(Patient::getCreateTime));
        return new PageResult(patientIPage.getTotal(), patientIPage.getRecords());
    }

    @Override
    public Result selectPatientByIdNumber(String idNumber) {
        Patient patient = this.patientMapper.selectOne(new LambdaQueryWrapper<Patient>()
                .eq(Patient::getIdNumber, idNumber));
        return new Result(patient);
    }
}
